幾天來介紹這個空氣盒子專案,所有元件與運作實際介紹。
基於高性能的濕度感應元件製成,新一代 HTU21D 溫度和濕度傳感器在尺寸與智能方面建立了新的標準:
HTU21D
HTU21D 實際裝配方式
安裝使用此程式庫SparkFun Humidity and Temperature Sensor Breakout - HTU21D。
範例程式碼:
#include <Wire.h>
#include "SparkFunHTU21D.h"
//Create an instance of the object
HTU21D myHumidity;
void setup()
{
Serial.begin(9600);
Serial.println("HTU21D Example!");
myHumidity.begin();
}
void loop()
{
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();
Serial.print("Time:");
Serial.print(millis());
Serial.print(" Temperature:");
Serial.print(temp, 1);
Serial.print("C");
Serial.print(" Humidity:");
Serial.print(humd, 1);
Serial.print("%");
Serial.println();
delay(1000);
}
之前有介紹到的 CC2541 藍芽模組我們使用 Serial 通訊,等等我們會介紹硬體不夠用的部分如何克服。
HTU21D 實際裝配方式
2.2 吋 UART 串口 HMI 液晶螢幕模組 TFT 液晶屏模組
HMI 人機介面,此款模組特點:
2.2 吋 UART 串口 HMI 液晶螢幕模組 TFT 液晶屏模組
HMI 液晶螢幕模組 軟體功能與畫面
HMI 液晶螢幕模組 軟體功能與畫面
昨天我們介紹過的主要感測元件 PMS5003。
PMS5003 激光散射原理 運作數值較穩定且較為可靠!
當時程式碼還不是很完整,沒有好用的程式庫。
#include <SoftwareSerial.h>
//RX ,TXm,false,buffer
//D5=14,D6=12
SoftwareSerial swSer(14, SW_SERIAL_UNUSED_PIN, false, 64);
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];
int cfPM01Value=0;
int cfPM2_5Value=0;
int cfPM10Value=0;
int atPM01Value=0;
int atPM2_5Value=0;
int atPM10Value=0;
int PN03Value=0; //0.1升空氣中直徑在 0.3um 的顆粒物個數
int PN05Value=0; //0.1升空氣中直徑在 0.5um 的顆粒物個數
int PN10Value=0; //0.1升空氣中直徑在 1.0um 的顆粒物個數
int PN25Value=0; //0.1升空氣中直徑在 2.5um 的顆粒物個數
int PN50Value=0; //0.1升空氣中直徑在 5.0um 的顆粒物個數
int PN100Value=0; //0.1升空氣中直徑在 10um 的顆粒物個數
void setup()
{
Serial.begin(115200); //use serial0
swSer.begin(9600);
}
void loop()
{
if(swSer.find(0x42)){ //start to read when detect 0x42
swSer.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
cfPM01Value=transmitData(buf,3,4);//標準顆粒物PM01
cfPM2_5Value=transmitData(buf,5,6);//標準顆粒物PM2.5
cfPM10Value=transmitData(buf,7,8);//標準顆粒物PM10
atPM01Value=transmitData(buf,9,10);//大氣環境下PM01
atPM2_5Value=transmitData(buf,11,12);//大氣環境下PM2.5
atPM10Value=transmitData(buf,13,14);//大氣環境下PM10
PN03Value=transmitData(buf,15,16);//大氣環境下PM10
PN05Value=transmitData(buf,17,18);//大氣環境下PM10
PN10Value=transmitData(buf,19,20);//大氣環境下PM10
PN25Value=transmitData(buf,21,22);//大氣環境下PM10
PN50Value=transmitData(buf,23,24);//大氣環境下PM10
PN100Value=transmitData(buf,25,26);//大氣環境下PM10
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.println("=====CF=====");
Serial.print("PM1.0: ");
Serial.print(cfPM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(cfPM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(cfPM10Value);
Serial.println(" ug/m3");
Serial.println("=====AT=====");
Serial.print("PM1.0: ");
Serial.print(atPM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(atPM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(atPM10Value);
Serial.println(" ug/m3");
Serial.println("=====Particle Number=====");
Serial.print("> 0.3um: ");
Serial.println(PN03Value);
Serial.print("> 0.5um: ");
Serial.println(PN05Value);
Serial.print("> 1.0um: ");
Serial.println(PN10Value);
Serial.print("> 2.5um: ");
Serial.println(PN25Value);
Serial.print("> 5um: ");
Serial.println(PN50Value);
Serial.print("> 10um: ");
Serial.println(PN100Value);
Serial.println();
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitData(unsigned char *thebuf,int HighB,int LowB)
{
int result;
result=((thebuf[HighB]<<8) + thebuf[LowB]); //count PM1.0 value of the air detector module
return result;
}
但是現在不一樣了剛剛有找到很方便的的程式庫[PMS Library(https://github.com/fu-hsi/PMS)
這樣看來範例簡單不少。
#include "PMS.h"
PMS pms(Serial);
PMS::DATA data;
void setup()
{
Serial.begin(9600); // GPIO1, GPIO3 (TX/RX pin on ESP-12E Development Board)
Serial1.begin(9600); // GPIO2 (D4 pin on ESP-12E Development Board)
}
void loop()
{
if (pms.read(data))
{
Serial1.print("PM 1.0 (ug/m3): ");
Serial1.println(data.PM_AE_UG_1_0);
Serial1.print("PM 2.5 (ug/m3): ");
Serial1.println(data.PM_AE_UG_2_5);
Serial1.print("PM 10.0 (ug/m3): ");
Serial1.println(data.PM_AE_UG_10_0);
Serial1.println();
}
// Do other stuff...
}
今天介紹空氣盒子專案實所有硬體感測元件與模組的介紹與使用。